package edu.moravian.entity;

import edu.moravian.managers.EnemyManager;
import edu.moravian.managers.OrdinanceManager;
import edu.moravian.math.Point2D;
import edu.moravian.math.Vector2D;
import java.util.Iterator;

/**
 *
 * @author ronaghan
 */
public class Tower extends Entity
{
    
    
    private static final double TOWER_MASS = 0.0;
    private double cooldownTime;
    private double fireRadius;
    private double timeUntilCanFire;
    
    private TowerProperties twrProps;

    public double getFireRadius()
    {
        return fireRadius;
    }
    
    public double getCooldownTime()
    {
        return cooldownTime;
    }
    
    public TowerProperties getTowerProps()
    {
        return twrProps;
    }
    
    
    
    public Tower(Point2D location, Vector2D heading, double radius, double cooldownTime, double fireRadius, String type, TowerProperties props)
    {
        super(location, heading, TOWER_MASS, radius, type);
        this.fireRadius = fireRadius;
        this.cooldownTime = cooldownTime;
        this.timeUntilCanFire = 0;
        this.twrProps = props;
    }
    
    public Tower(Tower tower)
    {
        super(new Point2D(tower.location), new Vector2D(tower.heading), tower.mass, tower.radius, tower.getType());
        this.fireRadius = tower.fireRadius;
        this.cooldownTime = tower.cooldownTime;
        this.timeUntilCanFire = 0;
        this.twrProps = tower.getTowerProps();
    }
    
    
    public void update(double delta, OrdinanceManager ordinanceManager, EnemyManager enemyManager)
    {
        Iterator<Enemy> enemiesInRange = enemyManager.getEnemiesInRange(location, fireRadius).iterator();
        if(enemiesInRange.hasNext())
        {
            
            Enemy enemy = enemiesInRange.next();
            
            double distSq = enemy.getLocation().distanceSq(location);

            Vector2D toEnemy = enemy.getLocation().minus(location).getNormalized();
            heading = new Vector2D(toEnemy);
            
            if(canFire() && distSq < (fireRadius*fireRadius))
            {
                fire(ordinanceManager, enemy, delta);
            }
        }
        
        timeUntilCanFire -= delta;
        
        
    }
    
    private void fire(OrdinanceManager ordinanceManager, Enemy enemy, double delta)
    {
        this.timeUntilCanFire = cooldownTime;
        
        Vector2D toEnemy = enemy.getLocation().scalePlus(delta, enemy.getVelocity()).minus(location).getNormalized();
        double bulletSpeed = 240;
        double bulletMass = 1.0;
        double bulletRadius = 2.0;
        
        Ordinance ordinance = new Ordinance(new Point2D(location), toEnemy, bulletSpeed, bulletMass, bulletRadius, "ordinance1");
        ordinanceManager.addOrdinance(ordinance);
        
    }         
    
    private boolean canFire()
    {
        if(timeUntilCanFire <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public void update(double delta)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    


    
}
